sample() function to create the numbers.
library(dplyr)
fancy_matrix <-
sample(1:1000, 8*8, replace = TRUE) |>
matrix(nrow = 8, ncol = 8)
fancy_matrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 115 428 558 747 643 40 569 380
## [2,] 103 850 731 99 898 601 763 666
## [3,] 874 92 102 982 12 669 617 797
## [4,] 156 436 558 128 364 467 627 469
## [5,] 994 160 93 506 579 134 425 903
## [6,] 352 264 918 70 528 535 418 798
## [7,] 976 388 413 283 589 793 542 455
## [8,] 805 14 622 833 355 211 700 889
terra::rast() function can be fed with matrices to
create a raster layer.
library(terra)
fancy_raster_layer <- terra::rast(fancy_matrix)
terra::plot(fancy_raster_layer)
The terra::rast() function can not only be used to
create raster data on the fly, which is also not very interesting.
Instead, we can use it to import already prepared data.
.tiff files in the
./data folder of the workshop directory.
getwd(). Setting is done with setwd().
immigrants_cologne <- terra::rast("./data/immigrants_cologne.tif")
+, -, or / operators.
Z-standardization can be applied using the terra::scale()
function.
# load all layers
immigrants_cologne <-
terra::rast("./data/immigrants_cologne.tif")
inhabitants_cologne <-
terra::rast("./data/inhabitants_cologne.tif")
# create proportation layer
immigrants_proportion <- immigrants_cologne / inhabitants_cologne
# scale data
immigrants_proportion_scaled <- terra::scale(immigrants_proportation)
immigrants_proportion_scaled[immigrants_proportion_scaled < 0] <- 0
immigrants_proportion_scaled[immigrants_proportion_scaled > 0] <- 1
library(tmap)
tm_shape(immigrants_proportion) +
tm_raster(palette = "-viridis")
tm_shape(immigrants_proportion_scaled) +
tm_raster(palette = "-viridis")